//
//  MeetingRoomViewController.m
//  FineNurse
//
//  Created by TT_UED on 2018/4/26.
//  Copyright © 2018年 TT_UED. All rights reserved.
//

#import "MeetingRoomViewController.h"
#import "MeetingRomeCollectionViewController.h"
#import "TabSettingViewController.h"
#import <Masonry.h>

@interface MeetingRoomViewController ()

@property(nonatomic, strong) UICollectionView *collectionView;

@property(nonatomic, assign) NSInteger currentCollectionCellIndex;

@property(nonatomic, strong) TabSettingViewController* cellOneView;



@end

@implementation MeetingRoomViewController


-(void)initCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 定义大小
    layout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - NavBarHeight);
    // 设置最小行间距
    layout.minimumLineSpacing = 0;
    // 设置垂直间距
    layout.minimumInteritemSpacing = 0;
    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置边缘的间距，默认是{0，0，0，0}
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    CGRect collectionRect = CGRectMake(0, NavBarHeight + 50, ScreenWidth,  ScreenHeight - NavBarHeight - 50);
    self.collectionView = [[UICollectionView alloc]initWithFrame:collectionRect collectionViewLayout:layout];
    self.collectionView.backgroundColor = UIColor.greenColor;
    //self.collectionView = [[UICollectionView alloc]initWithFrame:];
    //self.collectionView.collectionViewLayout = layout;
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    // 隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = YES;
    // 取消弹簧效果
    self.collectionView.bounces = YES;
        
}

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会议室";
    self.view.backgroundColor = UIColor.whiteColor;
    
    self.cellOneView = [[TabSettingViewController alloc]init];
    
    UIView *vHead =  [self getHeadTitleView];
    
    [self initCollectionView];
    [self.view addSubview:vHead];
    [self.view addSubview:self.collectionView];
    
    //    [NSIndexPath indexPathForRow:0 inSection:0];
    
    [vHead mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NavBarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.mas_equalTo(50);
    }];
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(vHead.mas_bottom);
        make.width.equalTo(self.view.mas_width);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    
    NSIndexPath *ip = [NSIndexPath indexPathForRow: 2 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:ip atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(UIView *) getHeadTitleView{
 
    UIView *v1 = [[UIView alloc] init];
    v1.backgroundColor = UIColor.whiteColor;
    
    // the underline
    UIView *line1 = [[UIView alloc] init];
    line1.backgroundColor = UIColor.blueColor;
    
    UIButton *btn1 = [[UIButton alloc]init];
    [btn1 setTitle:@"全部安排" forState:UIControlStateNormal];
    [btn1 setTitleColor:  UIColor.blueColor forState:UIControlStateNormal];
    [btn1 setTag:0];
    [btn1 addTarget:self action:@selector(headTitleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [[UIButton alloc] init];
    [btn2 setTitle:@"我的发起" forState:UIControlStateNormal];
    [btn2 setTitleColor: UIColor.blackColor forState:UIControlStateNormal];
    [btn2 setTag:1];
    [btn2 addTarget:self action:@selector(headTitleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn3 = [[UIButton alloc] init];
    [btn3 setTitle:@"待我审核" forState:UIControlStateNormal];
    [btn3 setTitleColor: UIColor.blackColor forState:UIControlStateNormal];
    [btn3 setTag:2];
    [btn3 addTarget:self action:@selector(headTitleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [v1 addSubview:btn1];
    [v1 addSubview:btn2];
    [v1 addSubview:btn3];
    [v1 addSubview:line1];

    
    
    UIEdgeInsets padding = UIEdgeInsetsMake(2, 10, 10, 10);
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(v1.mas_centerX);
        make.top.equalTo(v1.mas_top).offset(padding.top);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v1.mas_top).offset(padding.top);
        make.right.equalTo(btn2.mas_left).offset(-padding.left);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v1.mas_top).offset(padding.top);
        make.left.equalTo(btn2.mas_right).offset(padding.left);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn1.mas_centerX);
        make.top.equalTo(btn1.mas_bottom).offset(0);
        make.width.equalTo(btn1.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(2);
    }];
    
    
    return v1;
}

-(void)headTitleClick:(id)sender{
    UIButton *btn = (UIButton*) sender;
    NSInteger tag = btn.tag;
    
    NSIndexPath *ip = [NSIndexPath indexPathForRow: tag inSection:0];
    [self.collectionView scrollToItemAtIndexPath:ip atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    self.currentCollectionCellIndex = tag;
    [self updateTopBtnStat];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    switch (indexPath.row) {
        case 0:
            cell.backgroundColor = UIColor.redColor;
            [cell.contentView addSubview: self.cellOneView.view ];
            break;
        case 1:
            cell.backgroundColor = UIColor.greenColor;
            break;
        default:
            cell.backgroundColor = UIColor.blueColor;
            break;
    }
    
    
    return cell;
}


-(void)updateTopBtnStat{
    
    UIView *v1 = self.view.subviews[0];
    NSArray *arr = v1.subviews;
    
    if(arr.count == 4){
        UIView *line1 = arr[3];
        for(int i = 0; i < arr.count - 1; i++){
            UIButton *btn1 = arr[i];
            if(i != self.currentCollectionCellIndex){
                
                [btn1 setTitleColor: UIColor.blackColor forState:UIControlStateNormal];
            }
            else
            {
                [btn1 setTitleColor: UIColor.blueColor forState:UIControlStateNormal];
                [line1 mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(btn1.mas_centerX);
                    make.top.equalTo(btn1.mas_bottom).offset(0);
                    make.width.equalTo(btn1.mas_width).multipliedBy(0.6);
                    make.height.mas_equalTo(2);
                }];
                
                [UIView animateWithDuration:0.3 animations:^{
                    [self.view layoutIfNeeded];
                }];
                
            }
            
        }
     
        
        
        
  
    }

    
}

- (void)computeCurrentCellIndex:(CGPoint)contentOffset {
    NSInteger n = contentOffset.x /  ScreenWidth;
    NSLog(@" COMPUTE RESULT =  %ld",n);
    if(n != self.currentCollectionCellIndex){
        self.currentCollectionCellIndex = n;
        [self updateTopBtnStat];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self computeCurrentCellIndex:scrollView.contentOffset];
}


- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated{
    NSLog(@"898989 = %d", indexPath.row);
    
}
#pragma mark <UICollectionViewDelegate>

/*
 // Uncomment this method to specify if the specified item should be highlighted during tracking
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 // Uncomment this method to specify if the specified item should be selected
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
 }
 */

/*
 // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
 - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
 return NO;
 }
 
 - (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
 return NO;
 }
 
 - (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
 
 }
 */

@end
